const ProductService = require('../../services/admin/ProductSevervice');

const ProductController = {
    //发布新资源
    add: async (req, res) => {
        //图片
        const cover = req.file ? `/productuploads${req.file.filename}` : '';
        //解构内容
        const { title, author, detail, introduction } = req.body;
        await ProductService.add({
            title,
            detail,
            cover,
            introduction,
            editTime: new Date(),
            author,
        });
        res.send({
            ActionType: 'OK',
        });
    },
    //获取资源列表
    getList: async (req, res) => {
        const author = req.params.aid;
        const result = await ProductService.getList({ author });
        res.send({
            ActionType: 'OK',
            data: result,
        });
    },
    //删除资源
    delList: async (req, res) => {
        await ProductService.delList({ _id: req.params.uid });
        res.send({
            ActionType: 'OK',
        });
    },
    //根据资源id获取资源
    getListByID: async (req, res) => {
        const _id = req.params.id;
        const result = await ProductService.getListByID({ _id });
        res.send({
            ActionType: 'OK',
            data: result,
        });
    },
    //更新编辑
    updateList: async (req, res) => {
        //图片
        const cover = req.file ? `/productuploads${req.file.filename}` : '';
        //解构内容
        const { title, detail, introduction } = req.body;
        await ProductService.updateList({
            _id: req.params.oid,
            title,
            introduction,
            detail,
            cover,
            editTime: new Date(),
        });
        res.send({
            ActionType: 'OK',
        });
    },
};

module.exports = ProductController;
